package com.res.share.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.res.share.common.Constants;
import com.res.share.model.*;
import com.res.share.model.dto.ArticleModel;
import com.res.share.model.dto.MessageModel;
import com.res.share.model.dto.UserFollowModel;
import com.res.share.model.dto.UserStatisModel;
import com.res.share.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private IResCollectionService resCollectionService;

    @Autowired
    private IResHistoryService resHistoryService;

    @Autowired
    private IResDownService resDownService;

    @Autowired
    private IResStatisticsService resStatisticsService;

    @Autowired
    private IResUserService resUserService;

    @Autowired
    private IResUserScoreService resUserScoreService;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private IResArtService resArtService;

    @Autowired
    private IResFollowService resFollowService;

    @Autowired
    private IResReceiveService receiveService;

    @Autowired
    private IResSendService sendService;
    @Autowired
    private IResNoticeService resNoticeService;


    @Override
    public Map<String, Object> findCollectionAricle(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Page<ResCollection> page = new Page<>(pageNum, 10);
        IPage<ResCollection> resCollectionIPage = resCollectionService.findCollectionAricle(page, userId);
        resultMap.put("list", resCollectionIPage != null ? resCollectionIPage.getRecords() : null);
        resultMap.put("total", resCollectionIPage != null ? resCollectionIPage.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }

    @Override
    public Map<String, Object> findHistoryList(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Page<ResHistory> page = new Page<>(pageNum, 10);
        IPage<ResHistory> resHistoryPage = resHistoryService.findHistoryList(page, userId);
        resultMap.put("list", resHistoryPage != null ? resHistoryPage.getRecords() : null);
        resultMap.put("total", resHistoryPage != null ? resHistoryPage.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }

    @Override
    public Map<String, Object> findResDownList(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Page<ResDown> page = new Page<>(pageNum, 10);
        IPage<ResDown> resHistoryPage = resDownService.findResDownList(page, userId);
        resultMap.put("downList", resHistoryPage != null ? resHistoryPage.getRecords() : null);
        resultMap.put("total", resHistoryPage != null ? resHistoryPage.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }

    @Override
    public UserStatisModel findPersionDetail(Integer userId) {
        UserStatisModel userStatisModel=resStatisticsService.findPersionDetail(userId);
        if(userStatisModel!=null){
            String userType = userStatisModel.getUserType();
            String displayDes=null;
            switch (userType){
                case "0":
                    displayDes="普通会员";
                    break;
                case "1":
                    displayDes="白银会员";
                    break;
                case "2":
                    displayDes="黄金会员";
                    break;
                case "3":
                    displayDes="钻石会员";
                    break;
                default:
                    displayDes="管理员";
                    break;
            }
            userStatisModel.setDisplayDesc(displayDes);
            //查询是否签到了
            Integer count=resUserScoreService.findUserSignExits(userId);
            if(count!=null){
                userStatisModel.setUserSign(1);
            }else {
                userStatisModel.setUserSign(2);
            }
        }
        return userStatisModel;
    }

    @Override
    public ResUser findCurrentUserInfo(Integer userId) {
        ResUser currentUserInfo = resUserService.findCurrentUserInfo(userId);
        if(currentUserInfo!=null){
            String userType = currentUserInfo.getUserType()+"";
            String displayDes=null;
            switch (userType){
                case "0":
                    displayDes="普通会员";
                    break;
                case "1":
                    displayDes="白银会员";
                    break;
                case "2":
                    displayDes="黄金会员";
                    break;
                case "3":
                    displayDes="钻石会员";
                    break;
                default:
                    displayDes="管理员";
                    break;
            }
            currentUserInfo.setDisplayDesc(displayDes);
        }
        return currentUserInfo;
    }

    @Override
    public Map<String, Object> findMyjifenList(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Integer scoreTotal=0;
        //查询总共积分
        Object commonCache = cacheService.getCommonCache(Constants.CATE_BASE_KEY + userId);
        if(commonCache!=null && StringUtils.isNotEmpty(commonCache.toString())){
            scoreTotal=Integer.valueOf(commonCache.toString());
        }else {
            scoreTotal=resUserScoreService.findMyScoreTotal(userId);
            if(scoreTotal!=null){
                cacheService.setCommonCache(Constants.CATE_BASE_KEY + userId,scoreTotal);
            }
        }
        Page<ResUserScore> page = new Page<>(pageNum, 10);
        IPage<ResUserScore> resHistoryPage = resUserScoreService.findMyjifenList(page, userId);
        List<ResUserScore> records = resHistoryPage.getRecords();
        if(!CollectionUtils.isEmpty(records)){
            records.stream().forEach(resUserScore -> {
                Integer status = resUserScore.getStatus();
                resUserScore.setStatusName(status==0?"未兑换":"已兑换");
            });
        }
        resultMap.put("list", records);
        resultMap.put("total", resHistoryPage != null ? resHistoryPage.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        resultMap.put("scoreTotal", scoreTotal);
        return resultMap;
    }

    @Override
    public Map<String, Object> findMyReports(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Page<ArticleModel> page = new Page<>(pageNum, 10);
        IPage<ArticleModel> resHistoryPage = resArtService.findMyReports(page, userId);
        resultMap.put("reportList", resHistoryPage != null ? resHistoryPage.getRecords() : null);
        resultMap.put("total", resHistoryPage != null ? resHistoryPage.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }

    @Override
    public Integer findUserMoney(Integer userId) {
        return resUserService.getById(userId).getUserMoney();
    }

    @Override
    public Map<String, Object> likemeDetailList(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Page<UserFollowModel> page = new Page<>(pageNum, 10);
        IPage<UserFollowModel> likeMeList = resFollowService.likemeDetailList(page, userId);
        resultMap.put("likeMeList", likeMeList != null ? likeMeList.getRecords() : null);
        resultMap.put("total", likeMeList != null ? likeMeList.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }

    @Override
    public Map<String, Object> mylikeuserList(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Page<UserFollowModel> page = new Page<>(pageNum, 10);
        IPage<UserFollowModel> followList = resFollowService.mylikeuserList(page, userId);
        resultMap.put("followList", followList != null ? followList.getRecords() : null);
        resultMap.put("total", followList != null ? followList.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }

    @Override
    public Map<String, Object> mylikeuserresList(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Page<ResCollection> page = new Page<>(pageNum, 10);
        IPage<ResCollection> mylikeuserresListIPage = resFollowService.mylikeuserresList(page, userId);
        resultMap.put("list", mylikeuserresListIPage != null ? mylikeuserresListIPage.getRecords() : null);
        resultMap.put("total", mylikeuserresListIPage != null ? mylikeuserresListIPage.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }


    @Override
    public Map<String, Object> findSendBoxList(Integer userId, Integer pageNum) {
        //发件箱
        Map<String, Object> resultMap = new HashMap<>();
        Page<MessageModel> page = new Page<>(pageNum, 10);
        IPage<MessageModel> messageModelIPage = sendService.findSendBoxList(page, userId);
        resultMap.put("list", messageModelIPage != null ? messageModelIPage.getRecords() : null);
        resultMap.put("total", messageModelIPage != null ? messageModelIPage.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }


    @Override
    public Map<String, Object> findReceiveBoxList(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Page<MessageModel> page = new Page<>(pageNum, 10);
        IPage<MessageModel> messageModelIPage = receiveService.findReceiveBoxList(page, userId);
        resultMap.put("list", messageModelIPage != null ? messageModelIPage.getRecords() : null);
        resultMap.put("total", messageModelIPage != null ? messageModelIPage.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }

    @Override
    public Map<String, Object> findSystemMessageList(Integer userId, Integer pageNum) {
        Map<String, Object> resultMap = new HashMap<>();
        Page<MessageModel> page = new Page<>(pageNum, 10);
        IPage<MessageModel> messageModelIPage = resNoticeService.findSystemMessageList(page, userId);
        resultMap.put("list", messageModelIPage != null ? messageModelIPage.getRecords() : null);
        resultMap.put("total", messageModelIPage != null ? messageModelIPage.getTotal() : 0);
        resultMap.put("currentPage", pageNum);
        return resultMap;
    }
}
